<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <el-button type="primary" size="small" @click="openDialog(1,0)">添加权限</el-button>
      </page-tools>
      <!-- 表格 -->
      <el-table border row-key="id" :data="list">
        <el-table-column prop="name" align="center" label="名称" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="openDialog(2,scope.row.id)">添加</el-button>
            <el-button type="text" @click="editRow(scope.row.id)">编辑</el-button>
            <el-button type="text" @click="delRow(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <!-- 新增修改 -->
    <AddPerm ref="addPerm" @updateList="initData" />
  </div>
</template>
<script>
import AddPerm from '@/views/permission/components/addPerm.vue'
import { delPermissionApi, getPermissionListApi } from '@/api/permission'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'Permission',
  components: { AddPerm },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getPermissionListApi()
      // console.log(res)
      this.list = tranListToTreeData(res, '0')
    },
    openDialog(type, pid) {
      // type就是类型、pid就是级别
      this.$refs.addPerm.dialogShow = true
      // console.log(type, pid)
      this.$refs.addPerm.formData.type = type
      this.$refs.addPerm.formData.pid = pid
    },
    editRow(Id) {
      // console.log(Id)
      this.$refs.addPerm.getRowInfo(Id)
    },
    // 删除事件
    async delRow(Id) {
      // 1.弹出确认框
      try {
        await this.$confirm('确认删除吗')
      } catch (error) {
        return console.log(error)
      }
      // 2.删除请求
      await delPermissionApi(Id)
      // 3.提示
      this.$message.success('删除成功')
      // 4.更新
      this.initData()
    }
  }
}
</script>
<style scoped>

</style>
